import cv2
import os
import pyautogui
import numpy as np
import random

video_folder_path = "./"


video_files = [f for f in os.listdir(video_folder_path) if f.endswith(".mp4")]

stop_playing = False  # 鼠标点击


def mouse_callback(event, x, y, flags, param):
    global stop_playing
    if event == cv2.EVENT_LBUTTONDOWN:
        stop_playing = True


cv2.namedWindow("Video Player", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Video Player", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback("Video Player", mouse_callback)

sw, sh = pyautogui.size()
black_bg = np.zeros((sh, sw, 3), dtype=np.uint8)
new_size=(sw,sh)
while 1:
    for video_file in video_files:
        randi = random.randint(5, 10)
        for _ in range(3, randi):
            video_path = os.path.join(video_folder_path, video_file)

            cap = cv2.VideoCapture(video_path)

            if not cap.isOpened():
                print(f"Error file: {video_path}")
                continue

            width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            fps = cap.get(cv2.CAP_PROP_FPS)

            print(f"视频: {video_path} (Width: {width}, Height: {height}, FPS: {fps})")

            while True:
                ret, frame = cap.read()

                if not ret:
                    break
               # mid_w = int((sw - frame.shape[1]) / 2)
                black_bg=cv2.resize(frame,new_size)
                #black_bg[:frame.shape[0], mid_w:frame.shape[1] + mid_w] = frame
              #  black_bg[:frame.shape[0], frame.shape[1] ] = frame
               # black_bg=[1024,900,3]
                cv2.imshow("Video Player", black_bg)

                if cv2.waitKey(25) & 0xFF == ord("q") or stop_playing:
                    stop_playing = 1
                    break

            if stop_playing:
                break
        cap.release()

        if stop_playing:
            break
    if stop_playing:
        break

cv2.destroyAllWindows()
